<template>
	<view>
		<view v-if="isAdd" class="add-box box-ct">
			<view class="cu-form-group" @tap="sel_temp">
				<view class="title">使用模板：</view>
				<view class="iconfont icon-you"></view>
			</view>
			<view class="cu-form-group">
				<view class="title">标题：</view>
				<input placeholder="请输入标题" v-model="notify_temp_obj.title"></input>
			</view>
			<view class="cu-form-group box-ct">
				<view class="title">内容：</view>
				<view style="background-color: #FFFFFF;max-height: 700upx;padding-top: 50upx;">
					<textarea placeholder-style="color:#cccccc" :maxlength="txt_maxlength" v-model="notify_temp_obj.ct"
						:placeholder="txt_ct"
						style="width: 600upx;height: 600upx;max-height: 600upx;border: 2upx solid #CCCCCC;margin: 0 auto;padding: 10upx;" />
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">通知人：</view>
				<input placeholder="请输入通知人" v-model="notify_temp_obj.people"></input>
			</view>
			<view class="cu-form-group" @tap="add_opt">
				<view class="title">选项：</view>
				<view class="title" style="font-size: 50upx;color: #F43F3B;font-weight: bold;">+</view>

			</view>

			<view v-for="(item, index) in list_opt" :key="index">
				<ayslideLeft :item="item" :data="{index:index,item:item}" @delItem="delItem_opt">
					<view class="cu-form-group">

						<ayswitchR :themeColor="themeColor" :item="item" :switch="false"></ayswitchR>
						<view>{{item.ct}}</view>

					</view>
				</ayslideLeft>
			</view>


			<view v-if="!isSave">
				<!-- #ifdef MP-WEIXIN -->
				<view v-if="!hasLogin" style="margin-top: 100upx;">
					<button style="width: 400upx;" @tap="toGetUserInfo" class="cf-bgcolorTheme">保存</button>
				</view>
				<view v-else style="margin-top: 100upx;" @tap="toAdd">
					<button style="width: 400upx;" class="cf-bgcolorTheme">保存</button>
				</view>
				<!-- #endif -->
				<!-- #ifndef MP-WEIXIN -->
				<view style="margin-top: 100upx;" @tap="toAdd">
					<button style="width: 400upx;" class="cf-bgcolorTheme">保存</button>
				</view>
				<!-- #endif -->
			</view>


			<view v-else style="margin-top: 100upx;">
				<!-- #ifdef MP-WEIXIN -->
				<button open-type="share" style="width: 400upx;" class="cf-bgcolorTheme">去转发</button>
				<!-- #endif -->
			</view>


			<ayPopTips id="pop_opt" ref="pop_opt" title="请输入选择类型" type="center" @closeModal="closeModal_opt_types"
				@toConfirm="toConfirm_opt_types">
				<view v-for="(item, index) in opt_types" :key="index" class="cu-form-group">
					<ayswitchR :isGroup="true" :themeColor="themeColor" :index="index" :item="item"
						:switch="item.switch" @change="change_opt"></ayswitchR>
					<view>
						<view v-if="item.is_other" style="padding-right: 140upx;">{{item.ct}}</view>
						<input v-else placeholder="请输入选择内容" v-model="item.ct"></input>

					</view>

				</view>
			</ayPopTips>

		</view>
		<view v-else>

			<view class="cu-form-group">
				<view class="title">标题：</view>
				<view class="title">{{notify_temp_obj.title}}</view>
			</view>
			<view class="cu-form-group box-ct">
				<view class="title">内容：</view>
				<view style="background-color: #FFFFFF;">
					{{notify_temp_obj.ct}}
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">通知人：</view>
				<view class="title">{{notify_temp_obj.people}}</view>
			</view>


			<view v-for="(item, index) in list_opt" :key="index">
				<view class="cu-form-group">

					<ayswitchR :isGroup="true" :themeColor="themeColor" :index="index" :item="item"
						:switch="item.switch" @change="change_opt_sel"></ayswitchR>
					<view v-if="item.is_other"><input placeholder="请自行备注" v-model="item.ct_other"></input> </view>
					<view v-else>{{item.ct}}</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">参与人：</view>
				<input placeholder="请输入参与人" v-model="participate_obj.people"></input>
			</view>

			<view v-if="!isSave">
				<!-- #ifdef MP-WEIXIN -->
				<view v-if="!hasLogin" style="margin: 100upx 0;">
					<button style="width: 400upx;" @tap="toGetUserInfo" class="cf-bgcolorTheme">参与</button>
				</view>
				<view v-else style="margin: 100upx 0;" @tap="toAdd_party">
					<button style="width: 400upx;" class="cf-bgcolorTheme">参与</button>
				</view>
				<!-- #endif -->
				<!-- #ifndef MP-WEIXIN -->
				<view style="margin: 100upx 0;" @tap="toAdd_party">
					<button style="width: 400upx;" class="cf-bgcolorTheme">参与</button>
				</view>
				<!-- #endif -->
			</view>


			<view v-else style="margin-top: 100upx;">
				<!-- #ifdef MP-WEIXIN -->
				<button open-type="share" style="width: 400upx;" class="cf-bgcolorTheme">分享</button>
				<!-- #endif -->
			</view>
			<view class="cf-box party-list-box">
				<view style="background-color: #FFFFFF;" class="party-item-box party-item-head-box">参与情况：</view>
				<view v-for="(item, index) in list_party_z" :key="'z'+index"
					class="cf-item-hen-box party-item-box party-item-head-z-box">

					<view class="party-item">
						{{item.key}}
					</view>
					<view class="party-item">
						{{item.val}}
					</view>

				</view>

				<view class="cf-item-hen-box party-item-box party-item-head-box">
					<view class="party-item">
						头像
					</view>
					<view class="party-item">
						参与人
					</view>
					<view class="party-item">
						类型
					</view>
					<view class="party-item">
						时间
					</view>

				</view>
				<view v-for="(item, index) in list_participate" :key="index" class="cf-item-hen-box party-item-box">
					<view class="party-item">
						<image :src="item.avatarUrl" style="width: 80upx;height: 80upx;border-radius: 50%;"></image>

					</view>
					<view class="party-item">
						{{item.participants}}
					</view>
					<view class="party-item">
						{{item.ct_options}}
					</view>
					<view class="party-item">
						{{item.time}}
					</view>
				</view>


			</view>


		</view>

		<!-- <view v-for="(item, index) in list" :key="index">
			{{JSON.stringify(item)}}
		</view>
		<view @tap="del_date">
			删除用户以外数据
		</view> -->
		<ayAd :type="2" :showOffAt="true"></ayAd>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import storage from '@/store/storage.js'
	import ayAd from '@/components/ay-ad/ay-ad.vue';
	import cloud from '@/api/cloud.js'
	import comm from '@/api/comm.js'
	import conf from '@/api/conf.js'

	import preDupliClick from '@/api/preDupliClick.js'
	import timeConvert from '@/api/timeConvert.js'
	import ayswitchR from '../components/ay-switch/ay-switch-round.vue'
	import ayPopTips from "@/components/ay-pop/ay-pop-tips.vue"

	import ayslideLeft from '@/components/ay-operate/del_slideLeft.vue';

	export default {
		components: {
			ayAd,
			ayswitchR,
			ayPopTips,
			ayslideLeft,
		},
		watch: {

		},
		computed: {
			...mapState(['hasLogin'])
		},
		data() {
			return {
				list_participate: [],
				list_party_z: [],
				participate_obj: {
					people: '',
				},
				list_opt_pre: [], //模板原始值
				notify_temp_obj_pre: {}, //模板原始值
				isAdd: true, //是否添加
				userInfo: {},

				isSave: false,
				themeColor: '#33CCCC',
				sel_opt_types: {},
				opt_types: [{
						ct: '',
						val: 0,
						switch: true,
						is_other: false,
					},
					{
						ct: '自行备注选项',
						val: 1,
						switch: false,
						is_other: true,
					},
				],
				list_opt: [
					// {
					// 	ct : '请假',
					// 	is_other : false ,

					// },
					// {
					// 	ct : '其他',
					// 	is_other : true ,

					// },
				],
				txt_ct: '请输入内容',
				txt_maxlength: 1000,
				id: '', //消息Id
				id_temp: '', //模板Id
				notify_temp_obj: {
					title: '',
					ct: '',
					people: '',
				},
				list: [],
				name: '',
				enddate: '',
			};
		},
		// #ifdef MP-WEIXIN
		//微信小程序的分享
		onShareAppMessage: function(options) {
			let that = this;
			//分享拦不住
			//菜单分享
			if (options.from == 'menu') {

			}
			console.log(options);
			//转发按钮分享
			if (options.from == 'button') {

			}
			return {
				title: '通知',
				path: 'pagesScan/notify/notify?id=' + that.id
			}
		},

		//分享到朋友圈
		onShareTimeline: function() {

		},
		// #endif
		onLoad: function(options) {
			console.log('notify onLoad')
			console.log(options)
			let that = this;

			if (options.id) {
				that.id = options.id;
			}

			if (options.id_temp) {
				that.id_temp = options.id_temp;
			}
			that.setData_init();
		},
		onShow: function() {
			let that = this;

		},
		methods: {
			toAdd_party() {
				let that = this;
				let participate_obj = that.participate_obj;

				if (participate_obj.people.trim().length == 0) {
					that.$api.msg('请输入参与人');
					return;
				}

				let list_opt = that.list_opt;
				let opt_obj = {};
				for (let i = 0; i < list_opt.length; i++) {
					let item = list_opt[i];
					if (item.switch) {
						opt_obj = item;
						break;
					}

				}

				if (opt_obj.is_other) {
					if (opt_obj.ct_other.trim().length == 0) {
						that.$api.msg('请自行输入备注');
						return;
					}
				}


				that.toAdd_partyToYun(opt_obj);
			},
			async toAdd_partyToYun(opt_obj) {
				let that = this;
				// "id":1,
				// "id_user":1,
				// "id_options":1,
				// "id_notify":1,
				// "participants"：'参与者',
				// "time":"2021-06-30 00:22:22"
				let now = timeConvert.getTime();
				let userInfo = that.userInfo;
				let id_options = '';
				let ct_options = '';

				let participate_obj = that.participate_obj;

				id_options = opt_obj.id;
				if (opt_obj.is_other) {
					ct_options = opt_obj.ct_other;
				}

				let openId = userInfo.openId;
				let addObj_party = {
					id: comm.getGuid(),
					id_user: openId,
					id_options: id_options,
					id_notify: that.id,
					ct_options: ct_options,
					participants: participate_obj.people,
					time: now,
					userInfo: userInfo,
				}

				let whereSql = {
					openId: addObj_party.id_user,
				};

				//参与人首次登录存入用户信息
				const res_user = await that.getToYun(cloud.tb_name.user, whereSql);

				if (res_user.length > 0) {
					let _user = res_user[0];
					if (addObj_party.participants !== _user.people) {

						that.updateToYun(cloud.tb_name.user, addObj_party.userInfo, _user._id);
						delete _user._id; //删除_id字段，防止插入报错

						that.addToYun(cloud.tb_name.user_recd, _user);
					}

				} else {
					that.addToYun(cloud.tb_name.user, addObj_party.userInfo);
				}


				that.$store.dispatch("loginFun", addObj_party.userInfo);

				whereSql = {
					id_notify: addObj_party.id_notify,
					id_user: addObj_party.id_user,
				};

				const res_participate = await that.getToYun(cloud.tb_name.participate, whereSql);

				if (res_participate.length > 0) {
					let _participate = res_participate[0];
					if (addObj_party.id_options !== _participate.id_options) {

						that.updateToYun(cloud.tb_name.user, addObj_party, _participate._id);
						delete _participate._id; //删除_id字段，防止插入报错

						that.addToYun(cloud.tb_name.participate_recd, _participate);
					} else {
						that.$api.msg('已参与，请勿重复参与！');
						return;
					}

				} else {
					that.addToYun(cloud.tb_name.participate, addObj_party);
				}
				that.$api.msg('参与成功！！');
				//参与过后立即显示参与结果
				that.getByYun(addObj_party.id_notify);
				that.getcountByYun(addObj_party.id_notify);
				that.isSave = true;

			},
			del_date() {
				let that = this;

				that.removeallToYun(cloud.tb_name.options);
				that.removeallToYun(cloud.tb_name.notify_options);
				that.removeallToYun(cloud.tb_name.notify_temp);
				that.removeallToYun(cloud.tb_name.notify);
				that.removeallToYun(cloud.tb_name.participate);
			},
			delItem_opt(e) {
				let that = this;

				let index = e.data.index;
				let item = e.item;

				let list_opt = that.list_opt;
				uni.showModal({
					title: '温馨提示',
					content: '确认删除吗？',
					success: (e) => {
						if (e.confirm) {
							//本地删除
							//该方法不适用批量删除功能，因为删了一个index会随之改变
							that.list_opt.splice(index, 1);
							//that.removeToYun('colName',id)
						}
					}
				})
			},

			sel_temp() {
				let that = this;

				uni.navigateTo({
					url: '/pagesScan/notify/notify_temp',
				})

			},
			setData_init() {
				let that = this;
				if (that.hasLogin) {
					that.userInfo = storage.getUserInfo();
					let userInfo = that.userInfo;
					that.participate_obj.people = userInfo.people;
				}
				let id = that.id;

				if (id.length > 0) {
					that.getByYun(id);
					that.getcountByYun(id);
					that.get_participateByYun(id);
					
				}
				let id_temp = that.id_temp;
				if (id_temp.length > 0) {
					that.getByYun_tempId(id_temp, true);
				}
			},

			async get_participateByYun(id) {
				let that = this;
				let list_participate = list_participate;
				let whereSql = {
					id_notify: id,
				};
				const res_participate = await that.getToYun(cloud.tb_name.participate, whereSql);
				if (res_participate.length > 0) {
					let _participate = res_participate;
					for (let i = 0; i < _participate.length; i++) {
						let item = _participate[i];
						whereSql = {
							openId: item.id_user,
						};
						const res_user = await that.getToYun(cloud.tb_name.user, whereSql);
						if (res_user.length > 0) {
							let _user = res_user[0];
							item['avatarUrl'] = _user.avatarUrl;
						}
						whereSql = {
							id: item.id_options,
						};
						const res_options = await that.getToYun(cloud.tb_name.options, whereSql);
						if (res_options.length > 0) {
							let _options = res_options[0];
							item['ct_options'] = _options.ct;
						}
					}
					that.list_participate = _participate;
				}

			},
			async getcountByYun(id) {
				let that = this;
				let whereSql = {
					id: id,
				};

				const res_notify = await that.getToYun(cloud.tb_name.notify, whereSql);

				if (res_notify.length > 0) {
					let _notify = res_notify[0];
					whereSql = {
						id_notify_temp: _notify.id_notify_temp,
					};
					const res_notify_options = await that.getToYun(cloud.tb_name.notify_options, whereSql);

					let _opts = res_notify_options;
					for (let i = 0; i < _opts.length; i++) {
						let id_options = _opts[i].id_options;
						whereSql = {
							id_notify: id,
							id_options: id_options,
						};

						const res_participate = await that.getcountToYun(cloud.tb_name.participate, whereSql);
						
						_opts[i]['val'] = res_participate || 0;
						whereSql = {
							id: id_options,
						};
						const res_options = await that.getToYun(cloud.tb_name.options, whereSql);
						_opts[i]['key'] = '';
						if (res_options.length > 0) {
							let _options = res_options[0];

							_opts[i].key = _options.ct || 0;
						}
					}

					let z_obj_val = 0;
					_opts.forEach(item => {
						z_obj_val += item.val;
					})
					let z_obj = {
						key: '总计',
						val: z_obj_val,
					}
					_opts.push(z_obj);
					that.list_party_z = _opts;
				}


			},
			async getByYun(id) {
				let that = this;
				let whereSql = {
					id: id,
				};
				const res_notify = await that.getToYun(cloud.tb_name.notify, whereSql);
				if (res_notify.length > 0) {
					let _notify = res_notify[0];

					that.getByYun_tempId(_notify.id_notify_temp);
					that.isAdd = false;
				}
			},
			async getByYun_tempId(tempId, isUseTemp = false) {
				let that = this;

				let whereSql = {
					id: tempId,
				};
				const res_notify_temp = await that.getToYun(cloud.tb_name.notify_temp, whereSql);
				if (res_notify_temp.length > 0) {
					let _notify_temp = res_notify_temp[0];

					if (isUseTemp) {
						_notify_temp.people = ''; //防止使用模板时，显示最初建模板的人
						//使用本地缓存的名称
						if (that.hasLogin) {
							let userInfo = that.userInfo;
							_notify_temp.people = userInfo.people;
						}
					}

					that.notify_temp_obj = _notify_temp;
					that.notify_temp_obj_pre = _notify_temp; //模板原始值

					whereSql = {
						id_notify_temp: _notify_temp.id,
					};
					let fieldSql = {
						id_options: true,
						_id: false,
					};
					const res_notify_options = await that.getToYun(cloud.tb_name.notify_options, whereSql, fieldSql);
					if (res_notify_options.length > 0) {
						let list_opt = [];
						for (let index = 0; index < res_notify_options.length; index++) {
							whereSql = {
								id: res_notify_options[index].id_options,
							};
							const res_options = await that.getToYun(cloud.tb_name.options, whereSql);
							let options_obj = res_options[0];
							let _switch = false;
							//参与人默认选第一种
							if (index == 0) {
								_switch = true;
							}
							options_obj['switch'] = _switch;
							list_opt.push(options_obj);
						}

						that.list_opt = list_opt;
						that.list_opt_pre = list_opt; //模板原始值


					}

				}

			},
			async toAddToYun() {
				let that = this;
				let list_opt = that.list_opt;
				let list_opt_pre = that.list_opt_pre;

				let notify_temp_obj = that.notify_temp_obj;
				let notify_temp_obj_pre = that.notify_temp_obj_pre;
				let userInfo = that.userInfo;
				let id_temp = that.id_temp;
				let now = timeConvert.getTime();
				userInfo['people'] = notify_temp_obj.people;
				userInfo['time'] = now;

				let addObj = {
					id_notify_temp: comm.getGuid(),
					now: now,
					openId: userInfo.openId,
					id_notify: comm.getGuid(),
					userInfo: userInfo,
					list_opt: list_opt,
					list_opt_pre: list_opt_pre,
					notify_temp_obj: notify_temp_obj,
					notify_temp_obj_pre: notify_temp_obj_pre,
					id_temp: id_temp,
				}

				let whereSql = {
					openId: addObj.openId,
					//people :  notify_temp_obj.people ,
				};
				const res_user = await that.getToYun(cloud.tb_name.user, whereSql);

				if (res_user.length > 0) {
					let _user = res_user[0];
					if (addObj.notify_temp_obj.people !== _user.people) {

						that.updateToYun(cloud.tb_name.user, addObj.userInfo, _user._id);
						delete _user._id; //删除_id字段，防止插入报错

						that.addToYun(cloud.tb_name.user_recd, _user);
					}

				} else {
					that.addToYun(cloud.tb_name.user, addObj.userInfo);
				}


				that.$store.dispatch("loginFun", addObj.userInfo);


				if (addObj.id_temp.length > 0) {
					//使用了模板
					let is_same_temp = false;

					let notify_temp_obj = addObj.notify_temp_obj;
					let notify_temp_obj_pre = addObj.notify_temp_obj_pre;
					delete notify_temp_obj.people; //删除people字段，防止对比通知人
					delete notify_temp_obj_pre.people; //删除people字段，防止对比通知人
					let list_opt = addObj.list_opt;
					let list_opt_pre = addObj.list_opt_pre;

					let is_same_notify_temp = (JSON.stringify(notify_temp_obj) == JSON.stringify(notify_temp_obj_pre));
					let is_same_list_opt = (JSON.stringify(list_opt) == JSON.stringify(list_opt_pre));
					is_same_temp = is_same_notify_temp && is_same_list_opt;
					if (is_same_temp) {
						addObj.id_notify_temp = addObj.id_temp;
						//跟模板一样，未改变
						let notify_obj = {
							id: addObj.id_notify,
							id_notify_temp: addObj.id_notify_temp,
							people: notify_temp_obj.people,
							id_user: addObj.openId,
							time: addObj.now,
						}
						that.addToYun(cloud.tb_name.notify, notify_obj);
					} else {
						//在模板的基础上有变更
						if (!is_same_list_opt) {
							//在模板的基础上选项变更
							let list_opt_add = [];
							addObj.list_opt.forEach(item => {
								if (!item._id) {
									item.id = comm.getGuid();
									item['time'] = addObj.now;
									item['id_user'] = addObj.openId;
									item['ct_other'] = '';
									list_opt_add.push(item);
								}
							})
							that.addToYun(cloud.tb_name.options, list_opt_add);
						}

						that.toAddToYun_rela(addObj);

					}

				} else {
					//纯自己添加
					addObj.list_opt.forEach(item => {
						item.id = comm.getGuid();
						item['time'] = addObj.now;
						item['id_user'] = addObj.openId;
						item['ct_other'] = '';
					})
					that.addToYun(cloud.tb_name.options, addObj.list_opt);


					that.toAddToYun_rela(addObj);

				}



				that.id = addObj.id_notify;

				that.isSave = true;

			},
			toAddToYun_rela(addObj) {
				let that = this;
				let list_notify_options = [];
				addObj.list_opt.forEach(item => {
					let obj = {
						id_options: item.id,
						id_notify_temp: addObj.id_notify_temp,
						time: addObj.now,
					}
					list_notify_options.push(obj);
				})
				that.addToYun(cloud.tb_name.notify_options, list_notify_options);

				addObj.notify_temp_obj['id'] = addObj.id_notify_temp;
				addObj.notify_temp_obj['id_user'] = addObj.openId;
				addObj.notify_temp_obj['is_temp'] = 0;
				addObj.notify_temp_obj['is_temp_public'] = 0;
				addObj.notify_temp_obj['time'] = addObj.now;

				that.addToYun(cloud.tb_name.notify_temp, addObj.notify_temp_obj);


				let notify_obj = {
					id: addObj.id_notify,
					id_notify_temp: addObj.id_notify_temp,
					people: addObj.notify_temp_obj.people,
					id_user: addObj.openId,
					time: addObj.now,
				}
				that.addToYun(cloud.tb_name.notify, notify_obj);
			},
			toAdd() {
				let that = this;
				let list_opt = that.list_opt;

				let notify_temp_obj = that.notify_temp_obj;

				if (notify_temp_obj.title.trim().length == 0) {
					that.$api.msg('请输入标题');
					return;
				}

				if (notify_temp_obj.ct.trim().length == 0) {
					that.$api.msg(that.txt_ct);
					return;
				}

				if (notify_temp_obj.people.trim().length == 0) {
					that.$api.msg('请输入通知人');
					return;
				}

				if (list_opt.length < 2) {
					that.$api.msg('选项不少于2项');
					return;
				}



				that.toAddToYun();

			},

			change_opt(e) {
				let that = this;

				that.opt_types.forEach(item => {
					let index = that.opt_types.indexOf(item);
					if (index !== e.index) {
						item.switch = false;
					} else {
						item.switch = true;
					}
				})

				that.sel_opt_types = e.item;
			},
			change_opt_sel(e) {
				let that = this;

				that.list_opt.forEach(item => {
					let index = that.list_opt.indexOf(item);
					if (index !== e.index) {
						item.switch = false;
					} else {
						item.switch = true;
					}
				})

				that.sel_opt_types = e.item;
			},
			add_opt() {
				let that = this;
				that.$refs.pop_opt.open(); //弹出确认框

			},
			//弹出框
			toConfirm_opt_types() {
				let that = this;

				let ct = '';
				let obj = {
					ct: ct,
					is_other: false,
				};

				let opt_types = that.opt_types;
				for (let i = 0; i < opt_types.length; i++) {
					let item = opt_types[i];
					if (item.switch) {
						ct = (item.ct).trim();
						obj.ct = ct;
						if (ct.length == 0) {
							that.$api.msg(that.txt_ct);
							return;
						}
						if (item.is_other) {
							obj = {
								ct: '其他',
								is_other: true,
							};
						}
						break;
					}

				}
				//是否已添加相同项
				for (let i = 0; i < that.list_opt.length; i++) {
					let item = that.list_opt[i];
					if (item.ct == obj.ct) {
						that.$api.msg('已存在，请重输！');
						return;

					}

				}


				that.list_opt.push(obj);
				this.$refs.pop_opt.close();
			},
			closeModal_opt_types() {
				this.$refs.pop_opt.close();
			},

			toGetUserInfo(e) {
				let that = this;
				let userInfo = {};
				if (!that.hasLogin) {
					uni.getUserProfile({
						desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							userInfo = res.userInfo,
								console.log(res)
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									//界面不能直接调起微信的openId
									let data = {
										code: loginRes.code,
										AppID_wx: conf.AppID_wx,
										AppSecret_wx: conf.AppSecret_wx,
										pre_url_wx: conf.pre_url_wx,
									};
									uniCloud.callFunction({
										name: 'z-code2Session',
										data: data,
									}).then((res) => {
										console.log(res)
										if (res.result.code === 0) {
											let openId = res.result.data.openid;
											userInfo['openId'] = openId;
											console.log('用户服务商唯一用户标识openId为：' + userInfo
												.openId);
											that.userInfo = userInfo;
											that.$store.dispatch("loginFun", userInfo);
											//保存登录信息至缓存
											if (that.isAdd) {
												that.toAdd();
											} else {
												that.toAdd_party();
											}
										}

									})
								},
								fail: function(err) {
									console.log('err', err)

									return uni.showToast({
										title: '微信登录失败',
										duration: 2000,
										icon: 'none'
									})

								}

							});


						}
					})

				}

			},

			addToYun(colName, data_add) {
				let that = this;
				let data = {
					colName: colName,
					data: data_add,
				};
				uniCloud.callFunction({
					name: 'add',
					data: data,
				})
			},
			async getcountToYun(colName, whereSql) {
				let that = this;
				let data = {
					colName: colName,
				};
				let str_whereSql = JSON.stringify(whereSql)
				let isWhereSql = (str_whereSql == "{}") || (str_whereSql == 'undefined');
				if (!isWhereSql) {
					data['where'] = whereSql;
				}

				const res = await that.$dateByCloud({
					name: 'getcount',
					data: data,
				})
				
				return res.result.total;
			},
			async getToYun(colName, whereSql, fieldSql) {
				let that = this;
				let data = {
					colName: colName,
				};
				let str_whereSql = JSON.stringify(whereSql)
				let isWhereSql = (str_whereSql == "{}") || (str_whereSql == 'undefined');
				if (!isWhereSql) {
					data['where'] = whereSql;
				}

				let str_fieldSql = JSON.stringify(fieldSql)
				let isFieldSql = (str_fieldSql == "{}") || (str_fieldSql == 'undefined');
				if (!isFieldSql) {
					data['field'] = fieldSql;
				}

				// uniCloud.callFunction({
				// 	name: 'get',
				// 	data: data,
				// }).then((res) => {
				// 	that.list = res.result.data;
				// })
				const res = await that.$dateByCloud({
					name: 'get',
					data: data,
				})
				return res.result.data;
			},
			updateToYun(colName, data_update, _id) {
				let that = this;
				let data = {
					colName: colName,
					data: data_update,
					_id: _id,
				};
				uniCloud.callFunction({
					name: 'update',
					data: data,
				})
			},
			removeToYun(colName, id) {
				let that = this;
				let data = {
					colName: colName,
					id: id,
				};
				uniCloud.callFunction({
					name: 'remove',
					data: data,
				}).then((res) => {
					that.getToYun(colName); //刷新页面
				})
			},
			removeallToYun(colName) {
				let that = this;
				let data = {
					colName: colName,
				};
				uniCloud.callFunction({
					name: 'removeall',
					data: data,
				})
			},

		},
	}
</script>

<style lang="scss">
	.add-box {}

	.party-list-box {


		.party-item-box {
			width: 100%;
			padding: 40upx;
			border-bottom: 2upx solid #F0F0F0;

			.party-item {
				width: 25%;
			}

		}

		.party-item-head-z-box:first-child {

			margin-top: 20upx;
		}

		.party-item-head-box {
			border-top-left-radius: 20upx;
			border-top-right-radius: 20upx;

			margin-top: 20upx;
		}

		.party-item-box:last-child {
			margin-bottom: 20upx;
			border-bottom-left-radius: 20upx;
			border-bottom-right-radius: 20upx;
		}
	}

	.box-ct {
		margin-bottom: 20upx;
	}
</style>
